<?php

namespace app\admin\controller;

use app\admin\controller\CommonController;
use think\Validate;
use think\Db;
use app\admin\model\ProvCityArea;
use app\util\File;

class TakecargoController extends CommonController {
    /*
     * 取货机列表
     * author xfwed 
     * date 2019.09.21
     */

    public function takeCargoList() {
        $city = input('param.city');
        $searchStr = input('param.search_str');
        $perPage = input('param.perPage');
        $page = input('param.page');

        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;
        $searchStr = trim($searchStr);
        if ($city) {
            $where['s.city'] = $city;
        }
        $where['m.is_delete'] = 0;
        $where['m.machine_type'] = 0;
        if ($searchStr) {
            $list = Db::table('da_machine_store_relation')->alias('m')
                            ->join('store s', 's.id = m.store_id', 'left')
                            ->where($where)
                            ->where(function ($query) use ($searchStr) {
                                $query->where('s.store_name', 'like', '%' . $searchStr . '%')
                                ->whereOr('m.charge_name', 'like', '%' . $searchStr . '%')
                                ->whereOr('m.charge_tel', 'like', '%' . $searchStr . '%');
                            })
                            ->field('m.robot_no,m.store_id,m.add_time,m.charge_name,m.charge_tel,s.store_name,s.city')
                            ->paginate($perPage)->toArray();
        } else {
            $list = Db::table('da_machine_store_relation')->alias('m')
                            ->join('store s', 's.id = m.store_id', 'left')
                            ->where($where)
                            ->field('m.robot_no,m.store_id,m.add_time,m.charge_name,m.charge_tel,s.store_name,s.city')
                            ->paginate($perPage)->toArray();
        }
        
        foreach ($list['data'] as $k => $v) {
            $list['data'][$k]['cityName'] = ProvCityArea::cityName($v['city']);
        }
        return $this->ajaxReturn(0, '查询成功', $list);
    }
    
    /*
     * 导入取货码
     * author xfwed
     * date 2020.07.07
     */

    public function importPickupCode() {
        $store_id = input('param.store_id');
        $item_id = input('param.item_id');
        if (!empty($_FILES)) {
            header("Content-Type:text/html;charset=utf8");

            $file = request()->file('importtxt');
            //echo "<pre/>";
//            print_r($file);die;
            $fileInfo = $file->move(ROOT_PATH . '../kkmimg/sv300/phpexcel', date('Ymdhis'));
            
            // 上传文件
            if (!$fileInfo) {
                $this->error('上传失败');
            }
            $fileName1 = 'sv300/phpexcel/' . str_replace(DS, '/', $fileInfo->getSaveName());
            $fileName = explode('.', $fileName1);
            //$fileName1="http://127.0.0.1/kkmimg/".$fileName1;
            $fileName1 = ROOT_PATH . '../kkmimg/' . $fileName1;
            //print_r($fileName1);

            $handle = fopen($fileName1, "r");//读取二进制文件时，需要将第二个参数设置成'rb'
            //通过filesize获得文件大小，将整个文件一下子读到一个字符串中
            $contents = fread($handle, filesize ($fileName1));
            fclose($handle);
            $contents = explode(',', $contents);
            for($i=0;$i<count($contents);$i++){
                $row = Db::table('da_ubox')->where('rece_code',$contents[$i])->find();
                if(empty($row)){
                    $data['item_id'] = $item_id;
                    $data['rece_code'] = $contents[$i];
                    $data['add_time'] = date('Y-m-d H:i:s');
                    $data['store_id'] = $store_id;
                    Db::table('da_ubox')->insert($data);
                }
            }
            return $this->ajaxReturn(0, '导入成功', null);
        } else {
            return $this->ajaxReturn(3, '请选择文件', null);
        }
    }
    
    /*
     * 取货码列表
     * author xfwed 
     * date 2020.07.07
     */

    public function pickupCodeList() {
        $item_id = input('param.item_id');
        $store_id = input('param.store_id');
        $is_used = input('param.is_used');
        $perPage = input('param.perPage');
        $page = input('param.page');

        $perPage = isset($perPage) ? $perPage : 10;
        $page = isset($page) ? $page : 1;

        if ($item_id) {
            $where['u.item_id'] = $item_id;
        }
        if ($store_id) {
            $where['u.store_id'] = $store_id;
        }
        if (isset($is_used)) {
            $where['u.is_used'] = $is_used;
        }
        $where= isset($where)?$where:[];
        $list = Db::table('da_ubox')->alias('u')
                ->join('item i','i.id = u.item_id','left')
                ->join('store s','s.id = u.store_id','left')
                ->where($where)
                ->field('u.*,i.item_name,s.store_name')
                ->paginate($perPage);
//        print_r(Db::table('da_ubox')->getLastSql());
        return $this->ajaxReturn(0, '查询成功', $list);
    }

}
